//
//  ShowBigImageView.m
//  PhotoLookManager
//
//  Created by ihope99 on 2017/8/4.
//  Copyright © 2017年 GZX. All rights reserved.
//

#import "ShowBigImageView.h"

#define kWidth    [UIScreen mainScreen].bounds.size.width
#define kHeight   [UIScreen mainScreen].bounds.size.height

static ShowBigImageView *mShowBigImageView = nil;

@implementation ShowBigImageView{
    UIScrollView *mPicScollView;
    NSInteger mIndex;
    NSArray *mArray;
    UIWindow *keyWindow;
    UIPageControl *mPageControl;
    
    CGRect keyFrame;
    
    UIView *bgView;
}


+(ShowBigImageView *)Share{
    if (!mShowBigImageView) {
        mShowBigImageView = [[ShowBigImageView alloc] init];
    }
    return mShowBigImageView;
}


-(instancetype)init{
    self = [super init];
    if (self) {
        
        keyWindow = [UIApplication sharedApplication].keyWindow;
        keyWindow.tag = 6666;
        
    }
    return self;
}

-(void)initPicScollView{
    
    
    bgView = [[UIView alloc]initWithFrame:keyFrame];
    bgView.backgroundColor = [UIColor blackColor];
    bgView.alpha = 0;
    [keyWindow addSubview:bgView];
    
    
    mPicScollView = [[UIScrollView alloc] initWithFrame:keyFrame];
    mPicScollView.delegate = self;
    mPicScollView.pagingEnabled = YES;
    mPicScollView.showsVerticalScrollIndicator = NO;
    mPicScollView.showsHorizontalScrollIndicator = NO;
    mPicScollView.userInteractionEnabled = YES;
    [keyWindow addSubview:mPicScollView];
    
    mPageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0,kHeight - 30, kWidth-10,20)];
    mPageControl.numberOfPages = mArray.count;
    mPageControl.currentPage = mIndex;
    [keyWindow addSubview:mPageControl];
    
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [mPicScollView addGestureRecognizer:tap];
    
    
    [UIView animateWithDuration:0.3 animations:^{
        bgView.frame = keyWindow.bounds;
        mPicScollView.frame = CGRectMake(0, 0, kWidth+10, kHeight);
        bgView.alpha = 1;
        mPicScollView.contentSize = CGSizeMake((kWidth+10)*mArray.count, kHeight);
    }];
    

}
-(void)tap{
    [self hide];
}
-(void)showWithArray:(NSArray *)array WithIndex:(NSInteger)index WithClickView:(UIView *)keyView{
    //获取之前图片的View
    keyFrame = [self GetRootViewFrame:keyView];
    mArray = array;
    mIndex = index;
    [self initPicScollView];
    mPageControl.numberOfPages = mArray.count;
    mPageControl.currentPage = index;
    if (array.count > 0) {
        mPageControl.hidden = NO;
    }
    else{
        mPageControl.hidden = YES;
    }
    
    
    for (NSInteger i = 0; i < mArray.count ; i ++ ) {
        SizeImageView *mSizeImageView = [[SizeImageView alloc]initWithFrame:CGRectMake((kWidth+10)*i, 0, kWidth, kHeight)];
        mSizeImageView.imagePath = mArray[i];
        [mPicScollView addSubview:mSizeImageView];
        
    }
    [mPicScollView setContentOffset:CGPointMake((kWidth+10)*mIndex, 0)];
}
-(CGRect)GetRootViewFrame:(UIView *)mView{
    CGRect tmp = [mView convertRect:mView.bounds toView:keyWindow];
    return tmp;
}


-(void)hide{
    
    [UIView animateWithDuration:0.3 animations:^{
        bgView.frame = keyFrame;
        bgView.alpha = 0;
        mPicScollView.frame = keyFrame;
        
    } completion:^(BOOL finished) {
        mPicScollView.delegate = nil;
        [bgView removeFromSuperview];
        [mPicScollView removeFromSuperview];
        [mPageControl removeFromSuperview];
    }];

}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger index = scrollView.contentOffset.x/kWidth;
    [mPageControl setCurrentPage:index];
}


@end

